/**
 * ReportGear(2011)
 */
package com.reportgear.designer.gui.component.date;

import java.awt.Dimension;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.Icon;
import javax.swing.JLabel;

import com.reportgear.core.util.ImageUtils;

/**
 * Day标签
 * 
 * @version 1.0 2011-4-27
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class JDayLabel extends JLabel {
	/**
	 * UID
	 */
	private static final long serialVersionUID = 4883796361730733685L;
	private static Icon todayIcon = ImageUtils.readIcon("/resources/images/date/today.gif");
	private Date date;
	private Icon currentIcon;
	final SimpleDateFormat dateFormat;
	final SimpleDateFormat dayFormat;

	public JDayLabel(Date date) {
		this(date, true);
	}

	public JDayLabel(Date date, boolean notToday) {
		this.date = null;
		this.currentIcon = null;
		this.dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		this.dayFormat = new SimpleDateFormat("d");
		setPreferredSize(new Dimension(40, 20));
		setToolTipText(this.dateFormat.format(date));
		this.date = date;
		if (notToday) {
			setHorizontalAlignment(JLabel.CENTER);
			setText(this.dayFormat.format(date));
			Date tmpDate = new Date();
			if (!(this.dateFormat.format(date).equals(this.dateFormat.format(tmpDate)))) {
				return;
			}
			this.currentIcon = todayIcon;
		} else {
			setText("今天:" + this.dateFormat.format(new Date()));
			setIcon(todayIcon);
			setHorizontalAlignment(JLabel.LEADING);
		}
	}

	public Date getDate() {
		return this.date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public void paint(Graphics g) {
		super.paint(g);
		if (this.currentIcon == null || !isEnabled()) {
			return;
		}
		int x = (getWidth() - this.currentIcon.getIconWidth()) / 2;
		int y = (getHeight() - this.currentIcon.getIconHeight()) / 2;

		this.currentIcon.paintIcon(this, g, x, y);
	}
}